package cn.appsys.controller.back;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.constants.Constants;
import cn.appsys.pojo.BackendUser;
import cn.appsys.service.back.BackService;

@Controller
@RequestMapping("/manager")
public class LoginController {
	@Resource
	private BackService backService;
	//登录入口
	@RequestMapping("/login")
	public String login() {
		return "backendlogin";
	}
	//验证该用户是否存在
	@RequestMapping("dologin")
	public String dologin(@RequestParam("userCode")String userCode,@RequestParam("userPassword")String userPassword,Model model,HttpSession session) {
		BackendUser backUser = backService.login_back(userCode);
		if(backUser == null) {
			model.addAttribute("error", "没有该用户");
			return "backendlogin";
		}else if(!backUser.getUserPassword().equals(userPassword)) {
			model.addAttribute("error", "密码错误");
			return "backendlogin";
		}else {
			//登录成功
			session.setAttribute(Constants.USER_SESSION,backUser);
			return "redirect:/back/main";
		}
	}
}
